package com.example.demo1122.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo1122.domain.LoginAdmin;
import com.example.demo1122.domain.ResponseResult;
import com.example.demo1122.entity.Admin;
import com.example.demo1122.mapper.AdminMapper;
import com.example.demo1122.mapper.TeacherforclassMapper;
import com.example.demo1122.service.IAdminService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2023-12-06
 */

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private TeacherforclassMapper teacherforclassMapper;
    @Mapper
    private AdminMapper adminMapper;
    @Override
    public ResponseResult changePassword(String newPassword) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginAdmin loginAdmin = (LoginAdmin) authentication.getPrincipal();
        String adminID = loginAdmin.getAdmin().getAdminID();
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("adminID", adminID);
        paramMap.put("newPassword", passwordEncoder.encode(newPassword));
        boolean result= baseMapper.changePassword(paramMap);
        if(result){
            return new ResponseResult(126, "修改成功");
        }
        else {
            return new ResponseResult(127,"修改失败");
        }
    }
    //删除教师与课堂联系
    @Override
    public boolean deleteTAFromClass(Integer classID, String teacherID) {
        // 调用mapper的删除方法
        int result = teacherforclassMapper.deleteByClassIdAndTeacherId(classID, teacherID);
        return result > 0;
    }

    @Override
    public ResponseResult getProfile(String adminID) {
        try {
            String name=baseMapper.getNameByID(adminID);
            System.out.println("ghnjmk"+adminID);
            Admin admin=new Admin();
            admin.setAdminID(adminID);
            
            admin.setName(name);

            System.out.println(adminID);
            return new ResponseResult(1,"success", admin);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(0,"error",e);
        }
    }
}

